# -*- coding:utf-8 -*
from django.shortcuts import render
from accounts.constants import *
from accounts.models import User
from helps.utility import get_object_or_none
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
import hashlib


#访问首页，判断session,存在则index.html,不存在返回login.html
@csrf_exempt
def index(request):
    user = request.session.get('user', None)
    if not user:
        return render_to_response('devops/login.html')
    return render_to_response('devops/index.html', {'username': user.name})


#访问服务器,判断session,存在则xxxx.html,不存在返回login.html
def server(request):
    user = request.session.get('user',None)
    if not user:
        return render_to_response('devops/login.html')
    return render_to_response('devops/server.html', {'username': user.name})

#用户页面的请求
def user(request):
    user = request.session.get('user',None)
    if not user:
        return render_to_response('devops/login.html')
    contact_list = User.objects.all()      # 获取表内数据
    paginator = Paginator(contact_list, 10) # 每页多少个
    page = request.GET.get('page')         # 获取page请求如user?page=xxx
    if page and page.isdigit():            # 判断如果page参数是否存在和者page是否为整数
        page = int(request.GET.get('page')) #int字符串转义成数字类似"123"转义123
    else:
        page = 1                            #默认值设置成1，后期将改成返回404页面
    if page >= 10:                          #列表页显示共10页,1-10,2-11类似等等
        user_info_list = paginator.page_range[page-10:page]
    else:
        user_info_list = paginator.page_range[0:10]
    try:
        contacts = paginator.page(page)     #打印出这页有多少个数据
    except PageNotAnInteger:                #不为整数返回第一页数据
          # If page is not an integer, deliver first page.
          contacts = paginator.page(1)
    except EmptyPage:
          # If page is out of range (e.g. 9999), deliver last page of results.
          contacts = paginator.page(paginator.num_pages)    #超过9999返回最后页数据
    return render_to_response('devops/user.html', {'username': user.name,"contacts": contacts,"plist": user_info_list,"page":page})

def release(request):
    user = request.session.get('user',None)
    if not user:
        return render_to_response('devops/login.html')
    return render_to_response('devops/release.html', {'username': user.name})


def user_logout(request):
    del request.session['user']
    return HttpResponseRedirect("/")




#检查登入用户
def _check_login(request):
      username = request.POST.get('username', None)
      password = request.POST.get('password', None)
      if not(username and password):    #判断如果用户名和密码是空则返回False
          return False
      user = get_object_or_none(User, name = username)  #调用orm会抛出异常，cacth这个异常，返回None
      if not user:                                      #调用的用户不存在则返回False
          return False
      if user.password != hashlib.md5(password).hexdigest(): #密码错误，或者不存在,返回False
          return False
      request.session['user'] = user                    #请求用户的session
      return True



@csrf_exempt
def user_login(request):
    if not _check_login(request):
        return HttpResponseRedirect('/')
    return HttpResponseRedirect('/')


#job管理
#def job_admin( request ):
#    user = request.session.get('user',None)
#    if not user:
#        return render_to_response('devops/login.html')
#    return render_to_response('devops/job.html', {'username': user.name})























